package com.lihui;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lihui.model.Student;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/04/02  19:08
 */
@WebServlet("/jackson")
public class JacksonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");

        //通过ObjectMapper可以实现json字符串 <---->  java对象
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
        student.setStudentId(student.getStudentId() + 100);
        student.setStudentName(student.getStudentName() + "_修改");


        resp.setContentType("application/json; charset=utf-8");
        String json = objectMapper.writeValueAsString(student);
        resp.getWriter().write(json);

    }
}
